/*
1. baseURL
2. 过期时间
3. 请求拦截
4. 响应拦截
*/
import axios from 'axios';
import store from '../store'
import { Toast } from 'antd-mobile';
//创建axios实例
const service = axios.create({
    baseURL: "http://81.71.65.4:3003/",
    timeout: 10 * 60 * 1000
})

//请求拦截
service.interceptors.request.use(
    config => {
        //token的设置
        const token = store.getState('user').user.token
        if (!(token === undefined || token === '')) {
            config.headers['user-token'] = token
        }
        return config;
    },
    err => {
        return Promise.reject(err);
    }
)


//响应拦截  , 报错信息提示
service.interceptors.response.use(
    res => {
        console.log('utils:', res);
        if (res.data.code === 666) {
            return res
        } else {
            Toast.show({
                icon: "fail",
                content: res.data.msg
            })
            return Promise.reject({
                message: res.data.msg
            })
        }

    },
    err => {
        return Promise.reject(err);
    }
)

//输出
export default service;

